package com.cz.fra.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.cz.fra.security.AuthorizationException;
import com.cz.fra.security.SecurityService;
import com.cz.fra.security.impl.SecurityServiceImpl;

public class LoginServlet extends HttpServlet{

	 /**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	SecurityService securityService = new SecurityServiceImpl();
	
	public void doGet(HttpServletRequest request,HttpServletResponse response) throws IOException,ServletException{
	     String name = request.getParameter("username").trim();
	     String pwd =  request.getParameter("password").trim();
	     PrintWriter out = response.getWriter();
	     try {
			if(securityService.getUser(name, pwd) != null) {
				 out.write("Success");
			 } else {
				 out.write("Failed");
			 }
		} catch (AuthorizationException e) {
			e.printStackTrace();
		}
	}
	
	public void doPost(HttpServletRequest request,HttpServletResponse response) throws IOException,ServletException{
		doGet(request,response);
	}
	
}
